package cn.tedu.tmall.admin.mall.handler;


import cn.tedu.tmall.common.enumerator.ServiceCode;
import cn.tedu.tmall.common.ex.ServiceException;
import cn.tedu.tmall.common.web.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.net.BindException;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {


    @ExceptionHandler
    public JsonResult handleServiceException(ServiceException e){

        return JsonResult.fail(e);
    }

    @ExceptionHandler
    public JsonResult handlerBindException(BindException e){
        String message = "请求参数格式错误";
        return JsonResult.fail(ServiceCode.ERROR_BAD_REQUEST,message);
    }

    @ExceptionHandler
    public JsonResult handlerThrowable(Throwable e){
        String message = "服务器忙，请稍后再试";
        //e.printStackTrace();生产环境中禁止使用
        log.warn("",e);
        return JsonResult.fail(ServiceCode.ERROR_UNKNOWN,message);

    }
}
